<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>仓库信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div>
      <el-form :inline="true" :model="selectForm" class="demo-form-inline" style="float: left">
        <el-form-item label="仓库名称">
          <el-input v-model="selectForm.stockName" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSelectClick(1,pageSize)">查询</el-button>
          <el-button type="danger" @click="onResetSelect">重置</el-button>
        </el-form-item>
      </el-form>

      <el-button type="success" @click="showEditFormDialogAddNew()" style="float: right;margin-bottom: 10px;">添加仓库</el-button>
      <el-dialog title="添加仓库" :visible.sync="dialogFormVisibleAddNew">
        <el-form :model="stock" label-width="100px">
          <el-form-item label="仓库名" >
            <el-input v-model="stock.stockName"></el-input>
          </el-form-item>

          <el-card>
            <v-distpicker :placeholders="placeholders"
                          @province="onChangeProvince"
                          @city="onChangeCity"
                          @area="onSelectArea"
                          @selected="onSelected">
            </v-distpicker>
          </el-card>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleAddNew = false">取 消</el-button>
          <el-button type="primary" @click="submitAddNewForm()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="仓库ID" width="100" align="center"></el-table-column>
        <el-table-column prop="stockName" label="仓库名称" header-align="center" ></el-table-column>
        <el-table-column prop="province" label="省份" header-align="center" ></el-table-column>
        <el-table-column prop="city" label="城市" header-align="center" ></el-table-column>
        <el-table-column prop="area" label="地区" header-align="center" ></el-table-column>
        <el-table-column prop="created" label="创建时间" header-align="center" ></el-table-column>
        <el-table-column prop="updated" label="修改时间" align="center"></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="showEditFormDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <el-dialog title="修改仓库" :visible.sync="dialogFormVisible">
        <el-form :model="editForm">
          <el-form-item label="仓库名称">
            <el-input v-model="editForm.stockName" autocomplete="off"></el-input>
          </el-form-item>
          <v-distpicker :placeholders="editForm.placeholders"
                        @province="onChangeProvince1"
                        @city="onChangeCity1"
                        @area="onSelectArea1"
                        @selected="onSelected1">
          </v-distpicker>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary"  @click="submitEditForm()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import VDistpicker from 'v-distpicker'
export default {
  name: '',
  components: { VDistpicker },
  data() {
    return {
      editForm:[],
      placeholders: {
        province: '------- 省 --------',
        city: '--- 市 ---',
        area: '--- 区 ---',
      },
      selectForm: {
        stockName: ''
      },
      tableData:[],
      dialogFormVisibleAddNew: false,
      dialogFormVisible: false,
      stock: {
        province:"",
        city:"",
        area:"",
      },
      total: 0,
      currentPage: 1,
      pageSizes: [5,10,15,20],
      pageSize: 10,
      isSelect: false,
    }
  },
  methods: {
    handleSizeChange(val) {
      if(this.isSelect){
        this.onSelectClick(this.currentPage,val);
      }else{
        this.pageSelect(this.currentPage,val);
      }
    },
    handleCurrentChange(val) {
      if(this.isSelect){
        this.onSelectClick(val,this.pageSize);
      }else{
        this.pageSelect(val,this.pageSize);
      }
    },
    pageSelect(page,pageSize){
      let url = 'http://localhost:8080/stock/select?page='+page+'&pageSize='+pageSize;
      this.axios.get(url).then((response)=>{
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          let data = jsonResult.data;
          this.tableData = data.list;
          this.currentPage = data.page;
          this.total = data.total;
        }else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      })
    },
    onSelectClick(page,pageSize){
      this.isSelect = true;
      let url = 'http://localhost:8080/stock/selectByCondition'
      let data = this.qs.stringify(this.selectForm);
      data = data+'&page='+page+'&pageSize='+pageSize;
      this.axios.post(url,data).then((response)=>{
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          let data = jsonResult.data;
          this.tableData = data.list;
          this.currentPage = data.page;
          this.total = data.total;
        }else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      })
    },
    onResetSelect(){
      this.selectForm.stockName='';
      this.isSelect = false;
      this.loadStockList();
    },
    openDeleteConfirm(stock){
      let message = '此操作将永久删除【' + stock.stockName + '】相册，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(stock);
      }).catch(() => {
      });
    },
    handleDelete(stock){
      let url='http://localhost:8080/stock/'+stock.id+'/delete';
      this.axios.post(url).then((response)=>{
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除' + stock.name + '成功！',
            type: 'success'
          });
          this.loadStockList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadStockList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      })

    },
    // 触发选择省份事件
    onChangeProvince1(data) {
      console.log(data)
      this.editForm.province = data.value
      console.log(this.placeholders.province)
    },
    // 触发选择城市事件
    onChangeCity1(data) {
      console.log(data)
      this.editForm.city = data.value
      console.log(this.placeholders.city)
    },
    // 触发选择地区事件
    onSelectArea1(data) {
      console.log(data)
      this.editForm.area = data.value
      console.log(this.placeholders.area)
    },
    // 选择最后一项时触发事件
    onSelected1(data) {
      console.log(data)
    },
    //根据id查询要修改的仓库
    showEditFormDialog(stock){
      this.dialogFormVisible=true;
      let url = 'http://localhost:8080/stock/' + stock.id;
      this.axios.get(url).then((response)=>{
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        }else if(jsonResult.state==40400){
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadStockList();
            }
          });
        }else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    //根据id修改仓库
    submitEditForm(){
      let url='http://localhost:8080/stock/'+this.editForm.id+'/update';
      let forData=this.qs.stringify(this.editForm);
      this.axios.post(url,forData).then((response)=>{
        let jsonResult=response.data
        if (jsonResult.state==20000){
          this.$message({
            message: '修改成功！',
            type: 'success'
          });
          this.dialogFormVisible=false;
          this.loadStockList();
        }else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    // 触发选择省份事件
    onChangeProvince(data) {
      console.log(data)
       this.stock.province = data.value
      console.log(this.placeholders.province)
    },
    // 触发选择城市事件
    onChangeCity(data) {
      console.log(data)
       this.stock.city = data.value
      console.log(this.placeholders.city)
    },
    // 触发选择地区事件
    onSelectArea(data) {
      console.log(data)
       this.stock.area = data.value
      console.log(this.placeholders.area)
    },
    // 选择最后一项时触发事件
    onSelected(data) {
      console.log(data)
    },
    loadStockList() {
      let url = 'http://localhost:8080/stock/select?page=1&pageSize='+this.pageSize;
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          let data = jsonResult.data;
          this.tableData = data.list;
          this.currentPage = data.page;
          this.total = data.total;

        }else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    //添加仓库
    submitAddNewForm(formName) {
      let url = 'http://localhost:8080/stock/add-new';

      let formData = this.qs.stringify(this.stock);

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '！',
            type: 'success'
          });
          this.dialogFormVisibleAddNew = false;
          this.loadStockList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    showEditFormDialogAddNew() {
      this.dialogFormVisibleAddNew = true;
    },
  },
  mounted() {
    this.loadStockList();
  }
}
</script>

